#include "widget.h"
#include "ui_widget.h"
#include "globalval.h"

Widget::Widget(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::Widget)
{
    ui->setupUi(this);

    setWindowTitle("智能儿童房");



    home =new Home;
    stm  = new Stm32;
    modbus =new Modbus;
    video =new Video;



    ui->stackedWidget->addWidget(home);
    ui->stackedWidget->addWidget(stm);
    ui->stackedWidget->addWidget(modbus);
    ui->stackedWidget->addWidget(video);
    QObject::connect(ui->listWidget, SIGNAL(currentRowChanged(int)),
                     ui->stackedWidget, SLOT(setCurrentIndex(int)));
    ui->listWidget->item(0)->setSelected(1);
//    ui->listWidget->item(1)->setHidden(1);
//    ui->listWidget->item(2)->setHidden(1);
//    ui->listWidget->item(3)->setHidden(1);
//    ui->listWidget->item(2)->setSelected(0);
//    ui->listWidget->item(3)->setSelected(0);
    MqttHandler *mqtt = new MqttHandler;
    GlobalVal::mqtt = mqtt;
    mqtt->setHost(QHostAddress("192.168.239.33"));
    mqtt->setPort(1883);
    mqtt->connectToHost();
//解析点表
//    QFileInfo fileInfo(":/new/prefix1/node.json");
//    qint64 fileSize=fileInfo.size();
//    QByteArray byte_array = document.toJson(QJsonDocument::Compact);
//    tsocket->write(byte_array);

//    QFile readFile(":/new/prefix1/node.json");
//    readFile.open(QIODevice::ReadOnly);
//    QByteArray arry;
//    while(!readFile.atEnd())
//    {
//        arry=readFile.read(fileSize);
//    }



    //哪个界面需要接收Mqtt的消息，就订阅这个信号，然后在自己的界面文件中处理消息
       QObject::connect(mqtt, SIGNAL(dataUpSignal(QByteArray)),
                         stm, SLOT(dataUpSlot(QByteArray)));
       QObject::connect(mqtt, SIGNAL(dataUpSignal(QByteArray)),
                         modbus, SLOT(dataUpSlot(QByteArray)));


}

Widget::~Widget()
{
    delete ui;
}
